package br.com.cast.scc.veiculo;

import java.util.HashSet;
import java.util.Set;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.validation.AbstractFormValidator;

public class PlacasIguaisValidator extends AbstractFormValidator {
	private static final long serialVersionUID = 1L;

	private FormComponent<String>[] components;
	
	public PlacasIguaisValidator(FormComponent<String>... components){
		this.components = components;
	}
	@Override
	public FormComponent<?>[] getDependentFormComponents() {
		return components;
	}

	@Override
	public void validate(Form<?> form) {
		Set<String> valores = new HashSet<>();
		
		for (FormComponent<String> component : components) {
			
			if(valores.contains(component.getInput())){
				error(component);
				break;
			}
			
			valores.add(component.getInput());
		}
		
	}

}
